<?php
namespace Model;

use Zend\Db\Adapter\Adapter;
use Zend\Db\Adapter\Platform\Mysql;
use Zend\Db\Adapter\Platform\Sql92;
use Zend\Db\Sql\Predicate\Like;
use Zend\Db\Sql\Predicate\Operator;
use Zend\Db\Sql\Select;
use Zend\InputFilter\InputFilter;

class CountryTable extends AbstractTable
{

    /**
     * @param $id
     * @return Country
     */
    public function getCountry($id){
       return $this->getById($id);
    }

    /**
     * @param $name
     * @return Country
     */
    public function getCountryByName($name)
    {
       return $this->getByColumn('name', $name)->current();
    }

    /**
     * @return InputFilter
     */
    public function getInputFilter()
    {
        $filter = new InputFilter();

// Adding a single input
        $filter->add(array(
            'name' => 'Mã',
            'required' => true,
            'validators' => array(
                array(
                    'name' => 'not_empty',
                ),
                array(
                    'name' => 'alpha',
                ),
                array(
                    'name' => 'string_length',
                    'options' => array(
                        'min' => 2,
                        'max' => 2
                    ),
                ),
            ),
        ));


        return $filter;
    }

    public function searchAllCountry($isPaging, $offset, $rowPerPage, $orderArr = array('name'), $searchOption){

        $select = $this->getDbSqlSelect();

        if($searchOption['name']){
            $select->where(array('name like "%'.$searchOption['name'].'%"'));
        }

        if($isPaging){
            $select->offset($offset)->limit($rowPerPage);
        }

        if($orderArr){
            $select->order($orderArr);
        }

        //generate sql string to debug.log file
        \Logger::debug($select->getSqlString($this->tableGateway->adapter->getPlatform()));

        $rows = $this->tableGateway->selectWith($select);
        $result['rows'] = $rows;

        $total = $this->getCount($select);

        $result['total'] = $total;

        return $result;


    }



}